package com.mqtt.IOT;

/**
 * @author wang
 * @version 1.0
 * @data 2024/3/28 下午 9:51
 * @desc 描述信息
 */



import com.alibaba.fastjson.JSONArray;
import com.mqtt.system.domain.IOTData;
import com.mqtt.system.mapper.IOTDataMapper;
import org.eclipse.paho.client.mqttv3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Arrays;

/**
 * mqtt回调处理类
 */

public class MqttConsumerCallback implements MqttCallbackExtended {


    private MqttClient client;
    private MqttConnectOptions options;
    private String[] topic;
    private int[] qos;
    private IOTDataMapper iotDataMapper;
    public static MqttConsumerCallback mqttConsumerCallback;//定义一个静态时实例


    public MqttConsumerCallback(MqttClient client, MqttConnectOptions options, String[] topic, int[] qos,IOTDataMapper iotDataMapper) {
        this.client = client;
        this.options = options;
        this.topic = topic;
        this.qos = qos;
        this.iotDataMapper = iotDataMapper;
    }

    /**
     * 断开重连
     */
    @Override
    public void connectionLost(Throwable cause) {
        System.out.println("MQTT连接断开，发起重连......");
        try {
            if (null != client && !client.isConnected()) {
                client.reconnect();
                System.out.println("尝试重新连接");
            } else {
                client.connect(options);
                System.out.println("尝试建立新连接");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 接收到消息调用令牌中调用
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        System.out.println("deliveryComplete---------" + Arrays.toString(topic));
    }

    /**
     * 消息处理
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) {
        try {
            String msg = new String(message.getPayload());
            System.out.println("收到topic:" + topic + " 消息：" + msg);
            // System.out.println("收到消息后执行具体的业务逻辑操作，比如将消息存储进数据库");
            IOTData iotData = JSONArray.parseObject(msg, IOTData.class);
            // System.out.println(iotData);
            // 插入数据
            // System.out.println(iotDataMapper+"------------------");

                this.iotDataMapper.insert(iotData);
            this.iotDataMapper.deleteOtherRecords();

        } catch (Exception e) {
            System.out.println("处理mqtt消息异常:" + e.getMessage());
        }
    }

    /**
     * mqtt连接后订阅主题
     */
    @Override
    public void connectComplete(boolean b, String s) {
        try {
            if (null != topic && null != qos) {
                if (client.isConnected()) {
                    client.subscribe(topic, qos);
                    System.out.println("mqtt连接成功，客户端ID：" + PropertiesUtil.MQTT_CLIENT_ID);
                    System.out.println("--订阅主题:：" + Arrays.toString(topic));
                } else {
                    System.out.println("mqtt连接失败，客户端ID：" + PropertiesUtil.MQTT_CLIENT_ID);
                }
            }
        } catch (Exception e) {
            System.out.println("mqtt订阅主题异常:" + e);
        }
    }
}